import pandas as pd

apiFile = pd.read_csv("data/api_nodes_estimator.csv", encoding='utf-8')
mashFile = pd.read_csv("data/mashup_nodes_estimator.csv", encoding='utf-8')
edgeFile = pd.read_csv("data/m-a_edges.csv", encoding='utf-8')

apiDf = pd.DataFrame(apiFile)
mashDf = pd.DataFrame(mashFile)
edgeDf = pd.DataFrame(edgeFile)
# print(df)
# print("Web API 总数为：" + str(apiDf.shape[0]))
# print("Mushup 总数为：" + str(mashDf.shape[0]))


# 统计每个Mushup包含的Web API个数
mashup_api_num_Dict = {}
for mashName in edgeDf['source']:
    if mashup_api_num_Dict.get(mashName):
        mashup_api_num_Dict[mashName] = mashup_api_num_Dict[mashName] + 1
    else:
        mashup_api_num_Dict[mashName] = 1

mashup_api_num_DictDf = pd.DataFrame([mashup_api_num_Dict])
mashup_api_num_DictDf = mashup_api_num_DictDf.T
# print(mashup_api_num_DictDf)
# mashup_api_num_DictDf.to_csv('result/mashup_api_num.csv')


# 统计每个Web Api被使用次数
api_used_num_Dict = {}
for apiName in edgeDf['target']:
    if api_used_num_Dict.get(apiName):
        api_used_num_Dict[apiName] = api_used_num_Dict[apiName] + 1
    else:
        api_used_num_Dict[apiName] = 1
api_used_num_DictDf = pd.DataFrame([api_used_num_Dict])
api_used_num_DictDf = api_used_num_DictDf.T
# print(api_used_num_DictDf)
# api_used_num_DictDf.to_csv('result/api_used_num.csv')


# 统计Web API提供商发布Web API个数
api_url_num_Dict = {}
for i in range(1, 6):
    pathRoad = "data/raw/accessibility/api_accessibility/api_version_accessbiliby-" + str(i) + ".txt"
    with open(pathRoad) as f:
        content = f.readlines()
    for line in content:
        # print(line)
        tmpList = line.split('"')
        # print(tmpList)
        if "visit_url" in tmpList:
            if api_url_num_Dict.get(tmpList[-2]):
                api_url_num_Dict[tmpList[-2]] = api_url_num_Dict[tmpList[-2]] + 1
            else:
                api_url_num_Dict[tmpList[-2]] = 1

api_url_num_DictDf = pd.DataFrame([api_url_num_Dict])
api_url_num_DictDf = api_url_num_DictDf.T
print(api_url_num_DictDf)
# api_url_num_DictDf.to_csv('result/api_url_num.csv')
